package cn.jxau.fukin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

import cn.jxau.fukin.model.Admin;
import cn.jxau.fukin.model.Notice;
import cn.jxau.fukin.model.User;
import cn.jxau.fukin.service.IAdminService;
import cn.jxau.fukin.service.IFeedbackService;
import cn.jxau.fukin.service.INoticeService;
import cn.jxau.fukin.service.IUserService;

@Controller
@RequestMapping(value = "/admin")
public class AdminController {
	@Autowired
	private IAdminService adminService;
	@Autowired
	private IUserService userService;
	@Autowired
	private INoticeService noticeService;
	@Autowired
	private IFeedbackService feedbackService;

	@RequestMapping(value = "/showAdminPage.do")
	public String showAdminPage() {
		return "forward:/adminLogin.jsp";
	}

	@RequestMapping(value = "/adminLogin.do")
	public ModelAndView login(Admin admin, ModelAndView mv, HttpSession session, HttpServletRequest request)
			throws Exception {
		List<Admin> admininfo = adminService.queryAdminInfo(admin);
		if (admininfo != null && !admininfo.isEmpty()) {
			Admin ad = admininfo.get(0);
			session.setAttribute("admin", ad);
			mv.setViewName("admin/main");
		} else {
			session.setAttribute("msg", "登录失败");
			mv.setViewName("forward:/WEB-INF/jsp/adminLogin.jsp");
		}

		return mv;
	}

	@RequestMapping(value = "/userInfoSelect.do", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String userInfoSelect(@RequestParam(required = true, defaultValue = "1") Integer page,
			User user) {
		
		Gson gson = new Gson();
		return gson.toJson(userService.queryUserInfo(user,page));
	}
	
	@RequestMapping(value = "/selectByName.do", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String selectByName(String uName) {
		Gson gson = new Gson();
		return gson.toJson(userService.queryUserByName(uName));
	}

	@RequestMapping(value = "/sendNotice.do")
	@ResponseBody
	public String sendNotice( Notice notice, ModelAndView mv, HttpSession session) {
		return noticeService.addNotice((Admin) session.getAttribute("admin"), notice);
	}

	@ResponseBody
	@RequestMapping(value = "/showNotices.do", produces = "text/html;charset=UTF-8")
	public String showNotices() {
		Gson gson = new Gson();
		return gson.toJson(noticeService.selectNotice());
	}
	
	@ResponseBody
	@RequestMapping(value = "/showFeedback.do", produces = "text/html;charset=UTF-8")
	public String showFeedback() {
		Gson gson = new Gson();
		return gson.toJson(feedbackService.selectFeedbacks());
	}
	
	@ResponseBody
	@RequestMapping(value = "/updateNotice.do", produces = "text/html;charset=UTF-8")
	public String updateNotice(Notice notice,HttpSession session){
		return noticeService.updateNotice((Admin) session.getAttribute("admin"), notice);
	}
	
	@ResponseBody
	@RequestMapping(value = "/freezeUser.do", produces = "text/html;charset=UTF-8")
	public String freezeUser(String uId){
		userService.freezeUser(uId);
		return "success";
	}

}
